<?php

namespace app\common\job;

use app\common\model\PromotionEvent;
use app\common\model\PromotionGroup;
use app\index\dao\PromotionCallbackDao;
use ShidianUtil\Utils\TimeUtil;
use think\queue\Job;

/**
 * 回传任务
 * Class PromotionCallback
 * @package app\common\job
 */
class PromotionCallback
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        // $promotionActionId = $data['promotionActionId'];
        $promotionGroupId = $data['promotionGroupId'];
        $promotionEventId = $data['promotionEventId'];

        $this->implement($promotionEventId, $promotionGroupId, $data);

        $job->delete();

    }

    private function implement($promotionEventId, $promotionGroupId, $params): void
    {
        $group = PromotionGroup::where('id', $promotionGroupId)
            ->where('isDel', 0)
            ->find();
        if (is_null($group)) {
            return;
        }

        $event = PromotionEvent::where('id', $promotionEventId)
            ->find();
        if (is_null($event)) {
            return;
        }

        $params['bdToken'] = $group->bdToken;
        $params['bdNewType'] = $event->bdNewType;
        $params['eventType'] = $event->eventType;
        $params['promotionPlatform'] = $group->promotionPlatform;
        $params['result'] = 0;

        // 扣量信息
        $deduction = false;
        $deductionRound = 1;
        $deductionSeq = 0;
        $deductionBase = $params['deductionBase'];
        $deductionProportion = $params['deductionProportionOrigin'];

        if (!is_numeric($deductionProportion)) {
            $deductionProportion = 0;
        }

        if (!is_numeric($deductionBase)) {
            $deductionBase = 0;
        }

        if ($deductionProportion > 0 && $deductionBase > 0) {

            // 根据基数，获取最后1个回传记录的情况
            $last = \app\common\model\PromotionCallback::where('promotionGroupId', $group->id)
                ->where('promotionEventId', $event->id)
                ->where('created', '>=', TimeUtil::stamp(TimeUtil::DAY))
                ->order('deductionRound', 'DESC')
                ->order('deductionSeq', 'DESC')
                ->order('id', 'DESC')
                ->find();

            // 组数，次数
            if ($last) {
                $deductionRound = $last->deductionRound;
                $deductionSeq = $last->deductionSeq;
            }

            $deductionSeq += 1;

            if ($deductionSeq > $deductionBase) {
                $deductionSeq = 1;
                $deductionRound += 1;
            }

            $deductionIndex = bcsub($deductionBase, round($deductionBase * $deductionProportion));
            if ($deductionSeq > $deductionIndex) {
                $deduction = true;
            }
        }

        $params['deductionRound'] = $deductionRound;
        $params['deductionSeq'] = $deductionSeq;
        $params['deductionBase'] = $deductionBase;
        $params['deductionProportion'] = $deductionProportion;

        if ($deduction) {
            // 扣量

            $params['result'] = 3;
            $params['output'] = '';
        } else {

            // DB 百度 DY 抖音
            if ($params['promotionPlatform'] === 'BD') {
                if (is_blank($params['logidUrl'] ?? null)) {
                    return;
                }

                $response = $this->getOcpc()->uploadConvertData($params['bdToken'], $params['logidUrl'], $params['bdNewType']);

            } elseif ($params['promotionPlatform'] === 'DY') {
                if (is_blank($params['clickid'] ?? null)) {
                    return;
                }

                $response = $this->getOceanengine()->conversion($params['eventType'], $params['clickid']);
                // 0未知 1成功 2失败
            } else {
                return;
            }

            if ($response->isSuccess()) {
                $params['result'] = 1;
            } else {
                $params['result'] = 2;
            }
            $params['output'] = $response->getBody()->__toString();
        }

        // 记录结果
        $this->getPromotionCallbackDao()->insertActive($params);

    }

    private function getOcpc(): \baidu\Ocpc
    {
        return app(\baidu\Ocpc::class);
    }

    private function getOceanengine(): \oceanengine\Oceanengine
    {
        return app(\oceanengine\Oceanengine::class);
    }

    private function getPromotionCallbackDao(): PromotionCallbackDao
    {
        return app(PromotionCallbackDao::class);
    }
}
